
package net.swa.dewei.web.action;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Required;
import net.swa.dewei.beans.entity.ApkVersion;
import net.swa.dewei.service.ApkVersionService;
import net.swa.system.service.ICommonService;
import net.swa.system.util.annotation.Log;
import net.swa.system.web.action.AbstractBaseAction;
import net.swa.util.JsonResult;

/**电池信息表 Action**/
public class ApkVersionAction extends AbstractBaseAction
{
    private static final long serialVersionUID = -1541842392065452147L;

    private final Logger log = Logger.getLogger(ApkVersionAction.class);

    private ApkVersionService apkVersionService;

    /**文件服务器路径**/
    private String httpPath;

    private String name;

    //实体类id
    private Long id;

    //实体类
    private ApkVersion model;

    private String[] txtMsg;

    private File file;

    private String fileFileName;

    //id集合
    private Long[] ids;

    /** 公用方法接口 */
    private ICommonService commonService;

    /**初始化分页查询**/
    //    @Log(description = "进入版本信息分页查询")
    //    public String listPage()
    //    {
    //        model = apkVersionService.queryLastVersion();
    //        return "listPage";
    //    }

    @Log(description = "进入版本信息界面")
    public String index()
    {
        model = apkVersionService.queryLastVersion();
        if (null != model && !StringUtils.isBlank(model.getDetail()))
        {
            String[] ss = (model.getDetail()).split("@@");
            List<String> f = new ArrayList<String>();
            for (String s : ss)
            {
                f.add(s);
            }
            model.setFeature(f);
        }

        return "index";
    }

    public String queryPage() throws Exception
    {
        currentPage = currentPage == 0 ? 1 : currentPage;
        pageSize = pageSize == 0 ? 20 : pageSize;
        JsonResult<ApkVersion> json = null;
        //支持多种条件查询分页便于扩展
        json = apkVersionService.queryPage(name, currentPage, pageSize, orderBy, orderType);
        json.setCurrentPage(currentPage);
        outJson(json);
        return null;
    }

    public String queryLastVersion() throws Exception
    {
        model = apkVersionService.queryLastVersion();
        if (null != model && !StringUtils.isBlank(model.getDetail()))
        {
            String[] ss = (model.getDetail()).split("@@");
            List<String> f = new ArrayList<String>();
            for (String s : ss)
            {
                f.add(s);
            }
            model.setFeature(f);
        }
        outJson(model);
        return null;
    }

    @Log(description = "去新增或者修改版本页面")
    public String toAddVersion() throws Exception
    {
        log.debug("编辑广告信息");
        if (null != id)
        {
            model = apkVersionService.queryById(id);
            if (null != model && StringUtils.isBlank(model.getDetail()))
            {
                String[] ss = (model.getDetail()).split("@@");
                List<String> f = new ArrayList<String>();
                for (String s : ss)
                {
                    f.add(s);
                }
                model.setFeature(f);
            }
        }
        else
        {
            model = new ApkVersion();
        }
        return "toAddAd";
    }

    @Log(description = "去新增或者修改版本页面")
    public String save() throws Exception
    {
        Map<String, Object> map = null;
        if (null == model)
        {
            outError("数据丢失");
            return null;
        }
        if (null != file && (!file.toString().equals("")))
        {
            String detail = "";
            if (null != txtMsg || txtMsg.length > 0)
            {
                for (String m : txtMsg)
                {
                    if (!StringUtils.isBlank(m))
                    {
                        detail += m + "@@";
                    }
                }
                model.setDetail(detail);
            }
            String path = uploadFile();
            if (!StringUtils.isBlank(path))
            {
                model.setVerPath(path);
                if (null == model.getId())
                {
                    map = apkVersionService.add(model);
                }
                else
                {
                    map = apkVersionService.update(model);
                }

            }
            else
            {
                map = new HashMap<String, Object>();
                map.put("success", false);
                map.put("message", "文件上传失败");
            }
            outJson(map);
        }
        else
        {
            outError("没有选择文件上传");
        }
        return null;
    }

    /**
     * 上传文件
     * @return
     */
    private String uploadFile()
    {
        String realpath = ServletActionContext.getServletContext().getRealPath("/uploads/");
        try
        {
            File saveFile = new File(new File(realpath), fileFileName);
            if (saveFile.exists())
            {
                saveFile.delete();
            }
            file.renameTo(saveFile);
            return  httpPath+"uploads/" + fileFileName;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return "false";
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public ApkVersion getModel()
    {
        return model;
    }

    public void setModel(ApkVersion model)
    {
        this.model = model;
    }

    public Long[] getIds()
    {
        return ids;
    }

    public void setIds(Long[] ids)
    {
        this.ids = ids;
    }

    @Required
    @Resource
    public void setApkVersionService(ApkVersionService apkVersionService)
    {
        this.apkVersionService = apkVersionService;
    }

    public String[] getTxtMsg()
    {
        return txtMsg;
    }

    public void setTxtMsg(String[] txtMsg)
    {
        this.txtMsg = txtMsg;
    }

    public File getFile()
    {
        return file;
    }

    public void setFile(File file)
    {
        this.file = file;
    }

    public String getFileFileName()
    {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName)
    {
        this.fileFileName = fileFileName;
    }

    public ICommonService getCommonService()
    {
        return commonService;
    }

    @Required
    @Resource
    public void setCommonService(ICommonService commonService)
    {
        this.commonService = commonService;
    }

    public String getHttpPath()
    {
        return httpPath;
    }

    public void setHttpPath(String httpPath)
    {
        this.httpPath = httpPath;
    }
}
